package com.example.SMSTranslate;

import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

public final class smsInterceptor extends Service {
    private static final String CONTENT_SMS = "content://sms/out";
    //private CallerHistoryDataSource  database =  new  CallerHistoryDataSource(UCDGlobalContextProvider.getContext());
    static String messageId="";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        MyContentObserver contentObserver = new MyContentObserver(getBaseContext());
        ContentResolver contentResolver = getBaseContext().getContentResolver();
        contentResolver.registerContentObserver(Uri.parse(CONTENT_SMS),true, contentObserver);
        //Log.v("Caller History: Service Started.", "OutgoingSMSReceiverService");
    }

    @Override
    public void onDestroy() {
        //Log.v("Caller History: Service Stopped.", "OutgoingSMSReceiverService");    
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            return START_STICKY;
    }

    @Override
    public void onStart(Intent intent, int startid) {
        Log.v("Caller History: Service Started.", "OutgoingSMSReceiverService");
    }
    
    private class MyContentObserver extends ContentObserver {
        Context context;
        public MyContentObserver(Context context) {
            super(null);
            this.context = context;
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            Uri smsUri = Uri.parse(CONTENT_SMS);
            Cursor mCursor = context.getContentResolver().query(smsUri, null, null, null, null);
            mCursor.moveToNext();
            String msg = mCursor.getString(mCursor.getColumnIndex("body"));
            Log.v(msg,"Observer");
        }

        @Override
        public boolean deliverSelfNotifications() {
            return false;
        }
    }
}